package com.ruoyi.quality.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.CommonConstructionFile;
import com.ruoyi.quality.domain.FileInfo;
import com.ruoyi.quality.service.ICommonConstructionFileService;
import com.ruoyi.safe.domain.BizSafeAttach;
import com.ruoyi.safe.domain.BizSafeDocAttach;
import com.ruoyi.safe.domain.vo.BizSafeDocAttachVO;
import com.ruoyi.safe.mapper.BizSafeDocAttachMapper;
import com.ruoyi.safe.service.IBizSafeAttachService;
import com.ruoyi.safe.service.IBizSafeDocAttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 附件下载Controller
 * 
 * @author mahl2
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/quality/downLoadFiles")
public class CommonFileController extends BaseController
{
    @Autowired
    private IBizSafeAttachService bizSafeAttachService;
    @Autowired
    BizSafeDocAttachMapper bizSafeDocAttachMapper;
    /**
     * 下载图片附件列表
     */
    @Log(title = "下载图片附件列表", businessType = BusinessType.OTHER)
    @GetMapping("/downloadImg")
    public FileInfo downloadImg(String fileIds)
    {
        FileInfo fileInfo  = new FileInfo();
        if(StringUtils.isNotNull(fileIds) && !"".equals(fileIds)){
            String[] arr = fileIds.split(",");
            Long[] photoIds = new Long[arr.length];
            for(int i=0;i<arr.length;i++){
                photoIds[i] = Long.parseLong(arr[i]);
            }
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachService.selectBizSafeAttachByBizIds(photoIds);
            fileInfo.setFileImgArr(bizSafeAttaches);
        }
        return fileInfo;
    }

    /**
     * 下载附件附件列表
     */
    @Log(title = "下载附件附件列表", businessType = BusinessType.OTHER)
    @GetMapping("/downloadFile")
    public FileInfo downloadFile(String fileIds)
    {
        FileInfo fileInfo  = new FileInfo();
        Set<Long> set = new HashSet<>();
        if(fileIds!=null && !"".equals(fileIds) ){
            if(fileIds.contains(",")){
                // 遍历数组并添加到Set中
                for (String num : fileIds.split(",")) {
                    set.add(Long.parseLong(num));
                }
            }else{
                set.add(Long.parseLong(fileIds));
            }
            List<BizSafeDocAttach> bizSafeDocAttaches = bizSafeDocAttachMapper.selectBizSafeDocAttachByIds(set);
            fileInfo.setFileDocArr(bizSafeDocAttaches);
        }
        return fileInfo;
    }

}
